<template>
	<view class="buy">
		<view class="top">
			<text>猫之巢</text>
		</view>
		<scroll-view class="good-msg" style="max-height: 600rpx;min-height: 250rpx;" scroll-y="true">
			<view class="order-item" v-for="item in goodData">
				<view class="image">
					<image :src="(item!={})?'http://localhost:8088/file/downloadGoods?t='+item.image:''" mode="">
					</image>
					<view class="ball1"></view>
					<view class="ball2"></view>
				</view>
				<view class="msg">
					<view class="msg-left" @click="toDetail(item.goodId)">
						<text class="name">{{item.name}}</text>
						<text class="price">￥{{(item.amount/100).toFixed(2)}}</text>
						<text class="introduce">加入购物车时间：{{item.createTime}}</text>
						<text class="number">{{item.number}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="notes">
			<text>备注特殊要求:</text>
			<uni-easyinput type="textarea" style="margin-top: 20rpx;" autoHeight v-model="notes"
				placeholder="请输入内容"></uni-easyinput>
		</view>
		<view class="bottom">
			<view class="bottom-left">
				总金额：<span>￥</span>{{(mount/100).toFixed(2)}}
			</view>
			<view class="bottom-right" @click="buy">
				<text>结 算</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userId: '',
				notes: "",
				goodData: []
			}
		},
		computed: {
			mount() {
				let price = 0;
				for (let i = 0; i < this.goodData.length; i++) {
					price += (this.goodData[i].amount * this.goodData[i].number);
				}
				return price;
			}
		},
		onLoad() {
			this.userId = JSON.parse(localStorage.getItem("user"));
			console.log(this.userId);
			this.getCart()
		},
		methods: {
			toDetail(id) {
				uni.navigateTo({
					url: '/pages/details/details?id=' + id,
				})
			},
			getCart() {
				uni.showLoading({
					title: "加载中"
				})
				uni.request({
					url: '/cart/getAllCart',
					data: {
						id: this.userId
					},
					success: (res) => {
						console.log(res);
						this.goodData = res.data.data.records;
						uni.hideLoading();
					}
				})
			},
			buy() {
				uni.request({
					url: '/cart/buy',
					method: 'GET',
					data: {
						id: this.userId,
						remark: this.notes
					},
					success: (res) => {
						if (res.data.code == 200) {
							uni.showToast({
								title: "购买成功!",
								icon: "success"
							})
							setTimeout(function() {
								uni.switchTab({
									url: "/pages/order/order",
								});
							}, 2000);
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: "success"
							})
						}
					}
				})
			},
			clearCart() {
				uni.request({
					url: '/cart/removeCart',
					method: 'GET',
					data: {
						userId: this.userId,
					},
					success: (res) => {
						uni.showToast({
							title: res.data,
							icon: "success"
						})
					}
				})
			}
		}
	}
</script>

<style>
	.notes {
		color: gray;
		font-size: 30rpx;
		margin: 20rpx 40rpx;
	}

	.bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 100rpx;
		border-top: 2rpx solid rgba(0, 0, 0, 0.2);
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.bottom .bottom-left {
		padding-left: 20rpx;
	}

	.bottom .bottom-left span {
		font-weight: 600;
		color: #e89abe;
	}

	.bottom .bottom-right {
		width: 200rpx;
		background-color: #e89abe;
		line-height: 100rpx;
		text-align: center;
		color: white;
		font-weight: 600;
	}

	.top {
		font-size: 46rpx;
		font-weight: 600;
		padding: 20rpx;
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.2);
	}

	.order-item {
		width: 650rpx;
		margin-left: auto;
		margin-right: auto;
		display: flex;
		padding: 0 20rpx 0 0;
		margin-top: 10rpx;
		justify-content: left;
		align-items: center;
		border-radius: 20rpx;
		border: 2rpx solid rgba(0, 0, 0, 0.2);
	}

	.order-item .image {
		position: relative;
		box-sizing: border-box;
		padding: 10rpx;
		border-right: 4rpx dotted rgba(0, 0, 0, 0.2);
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.order-item .image .ball1,
	.order-item .image .ball2 {
		position: absolute;
		width: 20rpx;
		height: 20rpx;
		background-color: white;
		border-radius: 50%;
		right: -12rpx;
	}

	.order-item .image .ball1 {
		top: -16rpx;
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.2);
	}

	.order-item .image .ball2 {
		bottom: -16rpx;
		border-top: 2rpx solid rgba(0, 0, 0, 0.2);
	}

	.order-item image {
		height: 150rpx;
		width: 150rpx;
		border-radius: 20rpx;
	}

	.good-msg {
		border-bottom: 2rpx solid rgba(0, 0, 0, 0.2);
	}

	.msg {
		display: flex;
		justify-content: space-between;
		position: relative;
	}

	.order-item .msg text {
		margin-left: 20rpx;
		display: block;
	}

	.order-item .msg .name {
		width: 200rpx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		font-weight: 400;
	}

	.order-item .msg .number {
		font-size: 24rpx;
		position: absolute;
		right: 40rpx;
		top: 0;
		background-color: #e89abe;
		width: 30rpx;
		height: 30rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: white;
		border-radius: 50%;
		box-sizing: border-box;
		text-align: center;
	}

	.order-item .msg .introduce {
		color: gray;
		width: 450rpx;
		font-size: 24rpx;
		line-height: 40rpx;
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
	}

	.order-item .msg .price {
		text-align: left;
		font-weight: 600;
		width: 140rpx;
		color: #e89abe;
		text-shadow: 1rpx 1rpx 5rpx #e89abe;
	}

	.order-item .msg {
		align-items: center;
	}

	.order-item .msg button {
		align-items: center;
		font-size: 24rpx;
		height: 60rpx;
		display: block;
		float: right;
		background-color: #e89abe;
		color: white;
	}
</style>